﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFASimulation
{
    public class DFASystem
    {
        //private DFAState A, B, C;
        internal List<DFAState> Q;
        internal DFAState dummy = new DFAState(-1, "End");
        //private List<char> = new List<char>(2) { '0', '1' };

        public DFASystem() { }

        public DFASystem(List<DFAState> Q)
        {
            this.Q = Q;
            //A = new DFAState(1, "A");
            //B = new DFAState(2, "B");
            //C = new DFAState(3, "C");
            //createRules();
        }

        public void setStates(List<DFAState> Q)
        {
            this.Q = Q;
        }

        public void createTransitions(List<List<DFAState>> transitions)
        {
            if (transitions.Count == Q.Count)
            {
                int i=0;
                foreach (var trans in transitions)
                {
                    Q.ElementAt(i).next = trans;
                    i++;
                }
            }
        }

        public void createRules(List<char> rules)
        {
            foreach (var state in Q)
            {
                state.rules = rules;
            }
        }
  
        internal DFAState d(DFAState q, char[] W, int pos)
        {
            char a = ' ';
            DFAState currentState = q;
            if (currentState == dummy)
                return currentState;
            if (pos >= W.Length)
                return currentState;
            else
            {
                a = W[pos];
                try
                {
                    return d(q.getNext(a), W, pos + 1);
                }
                catch (ArgumentNullException)
                {
                    return d(dummy, W, pos + 1);
                }
            }
        }
    }
}
